//
//  XNCommunicationViewController.m
//  XINNingOA
//
//  Created by samuel on 2021/7/13.
//

#import "XNCommunicationViewController.h"
#import "XNCommunicationBillInfoModel.h"
#import "XNCommunicationBillInfoRequest.h"

@interface XNCommunicationViewController ()<UITableViewDelegate,UITableViewDataSource>

@property (nonatomic, strong) XNCommunicationBillInfoModel *infoModel;

@property (nonatomic, copy) NSArray *approveArrayC;

@end

@implementation XNCommunicationViewController

- (void)viewDidLoad {
    [super viewDidLoad];
    
    self.tableView.delegate = self;
    self.tableView.dataSource = self;
    
    [SVProgressHUD show];
    __weak typeof(self)weakSelf = self;
    dispatch_queue_t queue = dispatch_get_global_queue(DISPATCH_QUEUE_PRIORITY_DEFAULT, 0);
    dispatch_group_t group = dispatch_group_create();
    dispatch_group_enter(group);
    XNCommunicationBillInfoRequest *infoRequest = [[XNCommunicationBillInfoRequest alloc] initWithOrderId:self.orderId];
    [infoRequest startWithCompletionBlockWithSuccess:^(__kindof XNCommunicationBillInfoRequest * _Nonnull request) {
        weakSelf.infoModel = request.infoModel;
        __block NSMutableArray *arrayA = [NSMutableArray array];
        __block NSMutableArray *arrayC = [NSMutableArray array];
        [weakSelf.infoModel.historyList enumerateObjectsUsingBlock:^(XNBillApproveModel * _Nonnull obj, NSUInteger idx, BOOL * _Nonnull stop) {
            obj.checkAccountUserName = obj.checkAccountName;
            if ([obj.checkType isEqualToString:@"A"]) {
                obj.seq = arrayA.count + 1;
                [arrayA addObject:obj];
            }else{
                obj.seq = arrayC.count + 1;
                [arrayC addObject:obj];
            }
        }];
        
        
        weakSelf.approveArray = [NSArray arrayWithArray:arrayA];
        weakSelf.approveArrayC = [NSArray arrayWithArray:arrayC];
        
        dispatch_group_leave(group);
    } failure:^(__kindof XNCommunicationBillInfoRequest * _Nonnull request) {
        dispatch_group_leave(group);
    }];
    
//    dispatch_group_enter(group);
//    XNAdministrationAffExpApproveRequest *approveRequest = [[XNAdministrationAffExpApproveRequest alloc] initWithOrderId:self.orderId];
//    [approveRequest startWithCompletionBlockWithSuccess:^(__kindof XNAdministrationAffExpApproveRequest * _Nonnull request) {
//        weakSelf.approveArray = request.approveFlowArray;
//        dispatch_group_leave(group);
//    } failure:^(__kindof XNAdministrationAffExpApproveRequest * _Nonnull request) {
//        dispatch_group_leave(group);
//    }];
    
    dispatch_group_notify(group, queue, ^{
        [weakSelf creatTableData];

        dispatch_async(dispatch_get_main_queue(), ^{
            [SVProgressHUD dismiss];
            [weakSelf.tableView reloadData];
        });

    });
    
}


- (void)creatTableData
{
    XNUserModel *creatUser = self.infoModel.createUserObject;
    XNTableShowDataModel *infoModel = [[XNTableShowDataModel alloc] init];
    infoModel.showType = XNShowType_Normal;
    infoModel.title = @"单据信息";
    XNShowItemModel *itemModel = [[XNShowItemModel alloc] init];
    itemModel.showOverImage = self.infoModel.budgetItemObject.remainApplyMoney < 0;

    NSArray *showItemArray = [NSArray arrayWithObjects:
                              [NSString stringWithFormat:@"单据编号： %@",self.infoModel.ID],
                              [NSString stringWithFormat:@"制单日期： %@",self.infoModel.createTimeStr],
                              [NSString stringWithFormat:@"制单人： %@",creatUser.userName],
                              [NSString stringWithFormat:@"制单部门： %@",creatUser.departmentName],
                              [NSString stringWithFormat:@"职务： %@",creatUser.positionName],
                              nil];
    itemModel.showItemArray = showItemArray;
    [infoModel.showDataArray addObject:itemModel];
    [self.showArray addObject:infoModel];
    
    XNTableShowDataModel *submitApplyModel = [[XNTableShowDataModel alloc] init];
    submitApplyModel.showType = XNShowType_Normal;
    submitApplyModel.title = @"个人信息";
    XNShowItemModel *itemModel2 = [[XNShowItemModel alloc] init];
    NSArray *showItemArray2 = [NSArray arrayWithObjects:
                               [NSString stringWithFormat:@"申请人: %@",creatUser.userName],
                               [NSString stringWithFormat:@"申请部门： %@",self.infoModel.departmentName],
                               [NSString stringWithFormat:@"职务： %@",creatUser.positionName],
                               [NSString stringWithFormat:@"入职日期： %@",self.infoModel.inDateStr],
                               [NSString stringWithFormat:@"预计人数： %ld",self.infoModel.peopleCount],
                               [NSString stringWithFormat:@"客户名称： %@",self.infoModel.customerName],
                               [NSString stringWithFormat:@"币别： %@",@"人民币"],
                               [NSString stringWithFormat:@"是否暂支： %@",self.infoModel.isTemp ? @"是" :@"否"],
                               @"附件信息：",
                               nil];
    itemModel2.showItemArray = showItemArray2;
    itemModel2.fileArray = self.infoModel.fileList;
    itemModel2.fileIsImage = NO;
    [submitApplyModel.showDataArray addObject:itemModel2];
    [self.showArray addObject:submitApplyModel];
    
    
    
    if (self.infoModel.isTemp) {
        XNTableShowDataModel *reimbursementModel = [[XNTableShowDataModel alloc] init];
        reimbursementModel.showType = XNShowType_Normal;
        reimbursementModel.title = @"暂支单";
        XNShowItemModel *itemModel3 = [[XNShowItemModel alloc] init];
        NSArray *showItemArray3 = [NSArray arrayWithObjects:
                                   [NSString stringWithFormat:@"暂支事由： %@",self.infoModel.advanceDesc],
                                   [NSString stringWithFormat:@"暂支金额： %.3f",self.infoModel.advance],
                                   [NSString stringWithFormat:@"预计归还日期： %@",self.infoModel.advanceDateStr],
                                   nil];
        itemModel3.showItemArray = showItemArray3;
        [reimbursementModel.showDataArray addObject:itemModel3];
        [self.showArray addObject:reimbursementModel];
    }

    
    XNTableShowDataModel *remarkModel = [[XNTableShowDataModel alloc] init];
    remarkModel.showType = XNShowType_Normal;
    remarkModel.title = @"工作计划";
    [self.infoModel.itemList enumerateObjectsUsingBlock:^(XNCommunicationItemModel * _Nonnull obj, NSUInteger idx, BOOL * _Nonnull stop) {
        XNShowItemModel *temp = [[XNShowItemModel alloc] init];
        if([self.infoModel.status isEqualToString:@"BS090"]){
            temp.showItemArray = [NSArray arrayWithObjects:
                                  [NSString stringWithFormat:@"交际日期: %@",obj.communicationDateStr],
                                  [NSString stringWithFormat:@"交际类型: %@",obj.typeName],
                                  [NSString stringWithFormat:@"交际形式: %@",obj.wayName],
                                  [NSString stringWithFormat:@"交际说明: %@",obj.communicationContent],
                                  [NSString stringWithFormat:@"地点: %@",obj.address],
                                  [NSString stringWithFormat:@"客户姓名: %@",obj.name],
                                  [NSString stringWithFormat:@"客户职务: %@",obj.duty],
                                  [NSString stringWithFormat:@"客户等级: %@",obj.customerLevelStr],
                                  [NSString stringWithFormat:@"与公司来往职责范围: %@",obj.dutyRange],
                                  [NSString stringWithFormat:@"公司参加人员: %@",obj.attendPeople],
                                  [NSString stringWithFormat:@"金额: %.3f",obj.cost],
                                  [NSString stringWithFormat:@"报销金额: %.3f",obj.payCost],
                                  [NSString stringWithFormat:@"报销金额（不含税）: %.3f",obj.payCostNoDuty],
                                  [NSString stringWithFormat:@"税率: %@",obj.taxCode],
                                  [NSString stringWithFormat:@"附件张数: %ld",obj.fileCount],
                                  [NSString stringWithFormat:@"摘要: %@",obj.remark],
                                  nil];
        }else{
            temp.showItemArray = [NSArray arrayWithObjects:
                                  [NSString stringWithFormat:@"交际日期: %@",obj.communicationDateStr],
                                  [NSString stringWithFormat:@"交际类型: %@",obj.typeName],
                                  [NSString stringWithFormat:@"交际形式: %@",obj.wayName],
                                  [NSString stringWithFormat:@"交际说明: %@",obj.communicationContent],
                                  [NSString stringWithFormat:@"地点: %@",obj.address],
                                  [NSString stringWithFormat:@"客户姓名: %@",obj.name],
                                  [NSString stringWithFormat:@"客户职务: %@",obj.duty],
                                  [NSString stringWithFormat:@"客户等级: %@",obj.customerLevelStr],
                                  [NSString stringWithFormat:@"与公司来往职责范围: %@",obj.dutyRange],
                                  [NSString stringWithFormat:@"公司参加人员: %@",obj.attendPeople],
                                  [NSString stringWithFormat:@"金额: %.3f",obj.cost],
                                  nil];
        }

        [remarkModel.showDataArray addObject:temp];
    }];
    [self.showArray addObject:remarkModel];
    
    XNTableShowDataModel *showModel3 = [[XNTableShowDataModel alloc] init];
    showModel3.showType = XNShowType_Normal;
    showModel3.title = @"交际费用总计";
    XNShowItemModel *itemModel3 = [[XNShowItemModel alloc] init];
    NSArray *showItemArray3 = [NSArray arrayWithObjects:
                               [NSString stringWithFormat:@"申请金额： %.3f",self.infoModel.totalCost],
                               [NSString stringWithFormat:@"报销金额： %.3f",self.infoModel.totalPayCost],
                               [NSString stringWithFormat:@"差异金额： %.3f",self.infoModel.totalCost - self.infoModel.totalPayCost],
                               nil];
    itemModel3.showItemArray = showItemArray3;
    [showModel3.showDataArray addObject:itemModel3];
    [self.showArray addObject:showModel3];
    
    XNTableShowDataModel *showModel4 = [[XNTableShowDataModel alloc] init];
    showModel4.showType = XNShowType_Normal;
    showModel4.title = @"部门报销总计";
    XNShowItemModel *itemModel4 = [[XNShowItemModel alloc] init];
    NSArray *showItemArray4 = [NSArray arrayWithObjects:
                               [NSString stringWithFormat:@"本年度已报销交际费： %.3f",self.infoModel.budgetItemObject.reimbursedMoney],
                               [NSString stringWithFormat:@"本年度报销中交际费： %.3f",self.infoModel.budgetItemObject.reimbursingMoney],
                               nil];
    itemModel4.showItemArray = showItemArray4;
    [showModel4.showDataArray addObject:itemModel4];
    [self.showArray addObject:showModel4];
    
    XNTableShowDataModel *showModel5 = [[XNTableShowDataModel alloc] init];
    showModel5.showType = XNShowType_Normal;
    showModel5.title = @"预算信息";
    XNShowItemModel *itemModel5 = [[XNShowItemModel alloc] init];
    NSArray *showItemArray5 = [NSArray arrayWithObjects:
                               [NSString stringWithFormat:@"【合并核扣】交际费（%@）预算总金额:%.3f",self.infoModel.budgetItemObject.budgetInfo,self.infoModel.budgetItemObject.allMoney],
                               [NSString stringWithFormat:@"已报销： %.3f",self.infoModel.budgetItemObject.reimbursedMoney],
                               [NSString stringWithFormat:@"报销中： %.3f",self.infoModel.budgetItemObject.reimbursingMoney],
                               [NSString stringWithFormat:@"剩余可报销： %.3f",self.infoModel.budgetItemObject.remainReimburseMoney],
                               [NSString stringWithFormat:@"已申请未报销： %.3f",self.infoModel.budgetItemObject.applyedMoney],
                               [NSString stringWithFormat:@"在申请未报销： %.3f",self.infoModel.budgetItemObject.applyingMoney],
                               [NSString stringWithFormat:@"剩余可申请： %.3f",self.infoModel.budgetItemObject.remainApplyMoney],
                               nil];
    itemModel5.showItemArray = showItemArray5;
    [showModel5.showDataArray addObject:itemModel5];
    [self.showArray addObject:showModel5];
    
    
    XNTableShowDataModel *approveModelA = [[XNTableShowDataModel alloc] init];
    approveModelA.showType = XNShowType_Approve;
    approveModelA.title = @"审批流程";
    [self.approveArray enumerateObjectsUsingBlock:^(XNBillApproveModel *obj, NSUInteger idx, BOOL * _Nonnull stop) {
        XNShowItemModel *temp = [[XNShowItemModel alloc] init];
        temp.showItemArray = [NSArray arrayWithObject:obj];
        [approveModelA.showDataArray addObject:temp];
    }];
    [self.showArray addObject:approveModelA];
    
    if (self.approveArrayC.count > 0) {
        XNTableShowDataModel *approveModelC = [[XNTableShowDataModel alloc] init];
        approveModelC.showType = XNShowType_Approve;
        approveModelC.title = @"报销审核流程";
        [self.approveArrayC enumerateObjectsUsingBlock:^(XNBillApproveModel *obj, NSUInteger idx, BOOL * _Nonnull stop) {
            XNShowItemModel *temp = [[XNShowItemModel alloc] init];
            temp.showItemArray = [NSArray arrayWithObject:obj];
            [approveModelC.showDataArray addObject:temp];
        }];
        [self.showArray addObject:approveModelC];
    }

}

@end
